//
//  DYSimilarPicCell.m
//  PrivatePhoto
//
//  Created by PrivatePhoto Team on 2021.
//  Copyright © 2021年 PrivatePhoto Team. All rights reserved.
//

#import "DYSimilarPicCell.h"

#import "DYSimilarPicTopView.h"
#import "DYSimilarBigPicView.h"
#import "DYSimilarPicMutiView.h"

#import "DYSimilarPicModel.h"

@interface DYSimilarPicCell ()

@property (nonatomic, strong) DYSimilarPicTopView *topView;
@property (nonatomic, strong) DYSimilarBigPicView *bigPicView;
@property (nonatomic, strong) DYSimilarPicMutiView *mutiPicView;
@property (nonatomic, strong) DYSimilarPicCellModel *model;

@end

@implementation DYSimilarPicCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self configSubView];
        [self addObserverForModel];
    }
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    self.topView.frame = CGRectMake(0, 0, kScreenWidth, 50);
    CGFloat photoItemHeight = (kScreenWidth-4)/4;
    self.bigPicView.frame = CGRectMake(0, CGRectGetMaxY(self.topView.frame), kScreenWidth, 2*photoItemHeight);
    [self.mutiPicView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_offset(0);
        make.top.mas_equalTo(self.bigPicView.mas_bottom);
    }];
}

- (void)configSubView{
    
    [self.contentView addSubview:self.topView];
    [self.contentView addSubview:self.bigPicView];
    [self.contentView addSubview:self.mutiPicView];
  
}

- (void)addObserverForModel{
    
    @weakify(self)
    [RACObserve(self, model.seletedCount) subscribeNext:^(id  _Nullable x) {
        @strongify(self)
        NSString *title = self.model.seletedCount < self.model.allImgArray.count ? @"选择全部":@"取消全选";
        [self.topView.seletedBtn setTitle:title forState:UIControlStateNormal];
    }];
}

#pragma mark --- setter/getter

- (void)bindWithModel:(DYSimilarPicCellModel *)model{
    self.model = model;
   
    self.topView.picCountLabel.text = [NSString stringWithFormat:@"%ld张照片",(long)model.photosCount];
    self.bigPicView.bigImgItem = model.bigItem;
    self.bigPicView.imgArray = model.bigViewOthersArray;
    if (model.seletedArray.count > model.bigViewSeletedArray.count) {
        self.bigPicView.seletedArray = [[NSMutableArray alloc] initWithArray:model.bigViewSeletedArray];
        [self.bigPicView.seletedArray addObject:model.bigItem];
    }else{
        self.bigPicView.seletedArray = model.bigViewSeletedArray;
    }
    self.mutiPicView.imgArray = model.mutiViewImgArray;
    self.mutiPicView.seletedArray = model.mutiViewSeletedArray;
   
}

#pragma mark --- setter/getter

- (DYSimilarPicTopView *)topView{
    if (!_topView) {
        _topView = [[DYSimilarPicTopView alloc] init];
        @weakify(self)
        _topView.seletedBtnBlock = ^(BOOL isSelected) {
          @strongify(self)
            if (isSelected) {
                [self.model.seletedArray removeAllObjects];
                [self.model.bigViewSeletedArray removeAllObjects];
                [self.model.mutiViewSeletedArray removeAllObjects];
                [self.model.seletedArray addObjectsFromArray:self.model.allImgArray];
                [self.model.bigViewSeletedArray addObjectsFromArray:self.model.bigViewOthersArray];
                [self.model.mutiViewSeletedArray addObjectsFromArray:self.model.mutiViewImgArray];
                self.model.seletedCount = self.model.seletedArray.count;
               
            }else{
                [self.model.seletedArray removeAllObjects];
                [self.model.bigViewSeletedArray removeAllObjects];
                [self.model.mutiViewSeletedArray removeAllObjects];
                self.model.seletedCount = 0;
            }
            [self bindWithModel:self.model];
        };
    }
    return _topView;
}

- (DYSimilarBigPicView *)bigPicView{
    if (!_bigPicView) {
        _bigPicView = [[DYSimilarBigPicView alloc] init];
        @weakify(self)
        
        _bigPicView.lookDetailBlock = ^(DYSimilarPicModel *info, NSInteger index, NSArray *array) {
            @strongify(self)
            !self.lookDetailBlock ?:self.lookDetailBlock(info,index, array);
        };
        
        _bigPicView.SimilarBigPicViewBlock = ^(DYSimilarPicModel *info, BOOL isAdd,NSMutableArray *array) {
            @strongify(self)
            if ([self.model.seletedArray containsObject:info]) {
                if (isAdd) {return;}
                [self.model.seletedArray removeObject:info];
            }else{
                if (!isAdd) {return;}
                [self.model.seletedArray addObject:info];
            }

            if ([self.model.seletedArray containsObject:self.model.bigItem]) {
                self.model.seletedCount = self.model.seletedArray.count-1;
            }else{
                self.model.seletedCount = self.model.seletedArray.count;
            }
            
            !self.selectPhotoCompleteBlock ?:self.selectPhotoCompleteBlock(info,isAdd,self.model.seletedArray);
        };
    }
    return _bigPicView;
}

- (DYSimilarPicMutiView *)mutiPicView{
    if (!_mutiPicView) {
        _mutiPicView = [[DYSimilarPicMutiView alloc] init];
        @weakify(self)
        _mutiPicView.SimilarPicMutiViewBlock = ^(DYSimilarPicModel *info, BOOL isAdd, NSMutableArray *array) {
            @strongify(self)
            if ([self.model.seletedArray containsObject:info]) {
                if (isAdd) {return;}
                [self.model.seletedArray removeObject:info];
            }else{
                if (!isAdd) {return;}
                [self.model.seletedArray addObject:info];
            }
            if ([self.model.seletedArray containsObject:self.model.bigItem]) {
                self.model.seletedCount = self.model.seletedArray.count-1;
            }else{
                self.model.seletedCount = self.model.seletedArray.count;
            }
            
            !self.selectPhotoCompleteBlock ?:self.selectPhotoCompleteBlock(info,isAdd,self.model.seletedArray);
        };
    }
    return _mutiPicView;
}


@end
